<template>
  <div id="questionSubmitView">
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="questionId" label="ID" style="min-width: 240px">
        <a-input v-model="searchParams.questionId" placeholder="Enter ID" />
      </a-form-item>
      <a-form-item field="language" label="Language" style="min-width: 240px">
        <a-select
          v-model="searchParams.language"
          :style="{ width: '320px' }"
          placeholder="Choose language"
        >
          <a-option>JAVA</a-option>
          <a-option>C</a-option>
          <a-option>CPP</a-option>
          <a-option>PYTHON</a-option>
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">Search</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #status="{ record }">
        {{ getStatus(record.status) }}
      </template>
      <template #judgeInfo="{ record }">
        {{ transJudgeInfo(record.judgeInfo) }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="toQuestionPage(record)">
            Check
          </a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionSubmitQueryRequest,
} from "../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";

const tableRef = ref();

const mapStatus = new Map([
  [0, "Submitting"],
  [1, "Judging"],
  [2, "Completed"],
]);

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitQueryRequest>({
  questionId: undefined,
  language: undefined,
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await QuestionControllerService.listQuestionSubmitByPageUsingPost(
    {
      ...searchParams.value,
      sortField: "createTime",
      sortOrder: "descend",
    }
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
    dataList.value.forEach((questionInfo: any) => {
      questionInfo.status = getStatus(questionInfo.status);
    });
  } else {
    message.error("加载失败，" + res.message);
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
  // dataList.value = [
  //   // 当前页的记录
  //   {
  //     code: "A123", // 提交代码
  //     createTime: "2024-06-23T10:00:00Z", // 创建时间
  //     id: 1, // 提交记录ID
  //     judgeInfo: {
  //       // 判题信息
  //       memory: 1024, // 内存使用
  //       message: "AC", // 判题消息
  //       time: 50, // 时间使用
  //     },
  //     judgeResult: "AC", // 判题结果
  //     language: "Java", // 编程语言
  //     questionId: 101, // 问题ID
  //     status: 1, // 状态码
  //     userId: 1001, // 用户ID
  //     username: "user123", // 用户名
  //   },
  // ];
  // dataList.value[0].status = getStatus(dataList.value[0].status);
});

const columns = [
  {
    title: "ID",
    dataIndex: "id",
  },
  {
    title: "Language",
    dataIndex: "language",
  },
  {
    title: "JudgeInfo",
    slotName: "judgeInfo",
  },
  {
    title: "Status",
    dataIndex: "status",
  },
  {
    title: "QuestionId",
    dataIndex: "questionId",
  },
  {
    title: "userName",
    dataIndex: "username",
  },
  {
    title: "judgeResult",
    dataIndex: "judgeResult",
  },
  {
    slotName: "optional",
  },
  // {
  //   title: "createTime",
  //   slotName: "createTime",
  // },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const router = useRouter();

/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (question: Question) => {
  router.push({
    path: `/check/question/${question.id}`,
  });
};

/**
 * 确认搜索，重新加载数据
 */
const doSubmit = () => {
  // 这里需要重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
};

const getStatus = (status: number) => {
  return mapStatus.get(status);
};
const transJudgeInfo = (judgeinfo: object) => {
  console.log(dataList);
  let infostr1 = JSON.stringify(judgeinfo).slice(1, -1).replace(/"/g, "");
  return infostr1;
};
</script>

<style scoped>
#questionSubmitView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
